package com.ruoyi.pos.config;

import com.ruoyi.pos.websocket.DeviceDebugWebSocketHandler;
import com.ruoyi.pos.websocket.DeviceHeartbeatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * WebSocket配置类
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private DeviceDebugWebSocketHandler deviceDebugWebSocketHandler;

    @Autowired
    private DeviceHeartbeatWebSocketHandler deviceHeartbeatWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册设备调试WebSocket处理器
        registry.addHandler(deviceDebugWebSocketHandler, "/ws/device/debug")
                .setAllowedOrigins("*"); // 允许跨域
        
        // 注册设备心跳WebSocket处理器 - 供Android端使用
        registry.addHandler(deviceHeartbeatWebSocketHandler, "/ws/heartbeat/*")
                .setAllowedOrigins("*"); // 允许跨域
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
